﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    [ToolboxBitmap(typeof(ColorListBox), "YidanSoft.Library.EditorUtility.WinControls.ColorListBox.bmp")]
    public class ColorListBox : ListBox
    {
        private string[] colorArray = null;

        public ColorListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.ItemHeight++;
        }

        protected void DrawListBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            if (Index != -1)
            {
                Brush brush;
                if (selected)
                {
                    using (brush = new SolidBrush(ColorUtil.VSNetSelectionColor))
                    {
                        g.FillRectangle(brush, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
                    }
                    using (Pen pen = new Pen(ColorUtil.VSNetBorderColor))
                    {
                        g.DrawRectangle(pen, bounds.Left, bounds.Top, bounds.Width - 1, bounds.Height - 1);
                    }
                }
                else
                {
                    g.FillRectangle(SystemBrushes.Window, bounds.Left, bounds.Top, bounds.Width, bounds.Height);
                }
                string name = (string) this.Items[Index];
                Color color = Color.FromName(name);
                using (brush = new SolidBrush(color))
                {
                    g.FillRectangle(new SolidBrush(color), bounds.Left + 2, bounds.Top + 2, 20, bounds.Height - 4);
                }
                g.DrawRectangle(Pens.Black, new Rectangle(bounds.Left + 1, bounds.Top + 1, 0x15, bounds.Height - 3));
                g.DrawString(name, SystemInformation.MenuFont, SystemBrushes.ControlText, (PointF) new Point(bounds.Left + 0x1c, bounds.Top));
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle bounds = e.Bounds;
            bool selected = (e.State & DrawItemState.Selected) > DrawItemState.None;
            bool editSel = (e.State & DrawItemState.ComboBoxEdit) > DrawItemState.None;
            if (e.Index != -1)
            {
                this.DrawListBoxItem(g, bounds, e.Index, selected, editSel);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }

        public void PassMsg(ref Message m)
        {
            base.WndProc(ref m);
        }

        public string[] ColorArray
        {
            get
            {
                return this.colorArray;
            }
            set
            {
                this.colorArray = value;
                if (this.colorArray != null)
                {
                    this.Items.AddRange(value);
                }
            }
        }

        public ListBox.ObjectCollection Items
        {
            get
            {
                return base.Items;
            }
        }
    }
}

